#include "app_pool.h"
#include <mqueue.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdio.h>
static char*MQ_NAME = "/app_pool_mq";
static mqd_t mq;
static pthread_t *threads;
static int thread_num;

static void * thread_func(void * arg)
{
    (void)arg;
    Task task;
    while (1)
    {
        mq_receive(mq,(char*)&task,sizeof(Task),NULL);
        task.func(task.arg);
    }
    return NULL;
}
int app_pool_init(int size)
{
    // 初始化消息队列
    struct mq_attr attr;
    attr.mq_maxmsg = 10;
    attr.mq_msgsize = sizeof(Task);
    if ((mq = mq_open(MQ_NAME, O_CREAT | O_RDWR, 0666, &attr)) == -1)
    {
        perror("mq_open error");
        return -1;
    }

    //初始化线程池
    thread_num = size;
    threads = (pthread_t *)malloc(sizeof(pthread_t)*size);
    if(threads == NULL)
    {
        perror("malloc erro");
        mq_unlink(MQ_NAME);
        return -1;     
    }
    for (int i = 0; i < size; i++)
    {
        pthread_create(&threads[i],NULL,thread_func,NULL);
    }
    
    return 0;
}

int app_pool_close(void)
{
    for (int i = 0; i < thread_num; i++)
    {
        pthread_cancel(threads[i]);
        pthread_join(threads[i],NULL);
    }

    free(threads);
    mq_unlink(MQ_NAME);
    
    return 0;
}

int app_pool_register_task(int (*fun)(void *), void *argv)
{
    Task task ={
        .func=fun,
        .arg=argv
    };

    return mq_send(mq, (char *)&task, sizeof(Task), 0);
}
